<template>
	<view class="u-wrap">
		<view class="u-menu-wrap" v-if="tabbar.length!==0">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in tabbar" :key="index" class="u-tab-item"
					:class="[current==index ? 'u-tab-item-active' : '']" :data-current="index"
					@tap.stop="swichMenu(index,item.id,item.children)" style="width: 100%;">
					<text class="u-line-2">
						{{item.name}}
					</text>
				</view>
			</scroll-view>
				<scroll-view scroll-y class="right-box" v-if="chooseClassItem.length!=0||chooseClassList.length!==0">
					<view class="page-view" v-if="chooseClassItem.length!=0">
						<view class="class-item">
							<view class="item-container" >
								<view class="thumb-box" v-for="(item1, index1) in chooseClassItem" :key="index1"
									@click="toPage(item1)">
									<image class="item-menu-image" :src="item1.pureResourceUrl" mode="">
									</image>
									<view class="u-flex u-flex-col">
										<text class="item-menu-name u-font-24">{{ item1.brandName }}</text>
										<text class="item-menu-name u-font-24">{{ item1.abbreviation }}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view v-if="isHaveChild" class="page-view">
						<view class="class-item" v-for="(item1, index1) in chooseClassList" :key="index1">
							<view class="item-title" v-if="item1.items.length!=0">
								<text>{{item1.name}}</text>
							</view>
							<view class="item-container" v-if="item1.items.length!=0">
								<view class="thumb-box" v-for="(item2, index2) in item1.items" :key="index2"
									@click="toPage(item2)">
									<image class="item-menu-image" :src="item2.pureResourceUrl" mode="">
									</image>
									<view class="u-flex u-flex-col">
										<text class="item-menu-name u-font-24">{{ item2.brandName }}</text>
										<text class="item-menu-name u-font-24">{{ item2.abbreviation }}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
					
				</scroll-view>
				<view class="u-flex-col u-col-center u-row-center" style="height: 500rpx;width: 100%;" v-else>
					<u-empty text="该分类下没有关联商品"  ></u-empty>
				</view>
		</view>
		<view class="u-flex-col u-col-center u-row-center" style="height: 500rpx;width: 100%;" v-else>
			<u-empty text="该店铺下没有设置分类"  ></u-empty>
		</view>
	</view>
</template>

<script>
	import { useShopStore } from '@/store/shop.js';
	import { mapState } from 'pinia';
	export default {
		data() {
			return {
				tabbar: [],
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				chooseClassList:[],//有二级分类的列表
				chooseClassItem:[], //没有二级分类的列表
				isHaveChild:false, //判断是否有二级分类
				chooseBrand:{}, //当前选择的品牌商
				ownerShow:false,
			}
		},
		props:{
			SellerId:{
				type:String,
				default:""
			},
		},
		created() {
			this.getTreeList();
		},
		computed:{
			...mapState(useShopStore, {storeShop: 'shop'})
		},
		methods: {
			async toPage(item) {
				// 判断是否为用户
				if (!this.storeShop.id) {
					return this.$mHelper.toOtherApp(item.tmallUrl)
				}else{
					let auth = item.isPurchasable?1:0;
					this.$mRouter.push({ route: '/pages/index/xiangqing?id=' + item.id + '&sellerShopId=' + this.SellerId +'&auth=' + auth})
				}
				// 判断该商品是否只有一个卖家
				// const params = {
				// 	shopId: this.$store.getters['shop/id'],
				// 	//itemName: item.abbreviation,
				// 	itemId:item.id,
				// 	pageIndex: 1,
				// 	PageSize: 2
				// }
				// const data = await this.$http.get('api/Product/GetItemsApp', { params })
				// // if (!data) this.$mRouter.push({ route: '/pages/index/search?itemName=' + item.abbreviation })
				// if (!data||data.list==null) this.$mRouter.push({ route: '/pages/index/search?itemId=' + item.id+'&itemName=' +item.abbreviation });
				// if (data.list.length == 1) {
				// 	this.$mRouter.push({ route: '/pages/index/xiangqing?id=' + data.list[0].id + '&sellerShopId=' + data.list[0].sellerShopId })
				// } else {
				// 	// this.$mRouter.push({ route: '/pages/index/search?itemName=' + item.abbreviation })
				// 	this.$mRouter.push({ route: '/pages/index/search?itemId=' + item.id+'&itemName=' +item.abbreviation })
				// }
			},
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 获取左边分类列表（一级分类）
			async getTreeList() {
				let params = {
					includeChildren:true,
					includeItems:true,
					BuyerId:this.storeShop.id,
					SellerId:this.SellerId,
				}
				const res = await this.$newHttp.get('api/v1.2/ShanNiang/GetSellerCategories',{params:params});
				if(res.code == 200){
					this.tabbar = res.data;
					// if(res.data[0].items.length!==0){
					// 	this.chooseClassItem = res.data[0].items;
					// }
					this.itemInit(res.data[0].id,res.data[0].children,0);
				}else{
					this.$u.toast('获取分类失败')
				}
			},
			// 右边列表数据初始化
			itemInit(id,chilrenList,index){
				if(chilrenList.length!=0){
					this.isHaveChild = true;
					this.getChooseClass(id);
				}else{
					this.chooseClassList = [];
				}
				this.chooseClassItem = this.tabbar[index].items;
			},
			// 点击左边的栏目切换
			async swichMenu(index,id,chilrenList) {
				if (index == this.current) return;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
				this.itemInit(id,chilrenList,index);
			},
			// 获取有二级分类的右边列表数据
			async getChooseClass(id){
				uni.showLoading({
					title:'loading...'
				});
				let params = {
					parentId:id,
					includeChildren:false,
					includeItems:true,
					BuyerId:this.storeShop.id,
					SellerId:this.SellerId,
				}
				const res = await this.$newHttp.get('api/v1.2/ShanNiang/GetSellerCategories',{params:params});
				uni.hideLoading({
					title:'loading...'
				})
				if(res.code == 200){
					this.chooseClassList = res.data;
				}else{
					this.$u.toast('获取分类列表失败')
				}
			},
			// 获取没有二级分类的右边列表数据
			async getClassItems(id){
				uni.showLoading({
					title:'loading...'
				});
				let params = {
					id:id,
					PageIndex:1,
					PageSize:999,
					SellerId:this.SellerId,
				}
				const res = await this.$newHttp.get('api/v1.2/ShanNiang/GetSellerCategories',{params:params});
				uni.hideLoading({
					title:'loading...'
				})
				if(res.code == 200){
					this.chooseClassItem = res.data.list?res.data.list:[];
				}else{
					this.$u.toast('获取分类列表失败')
				}
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.owners-btn{
		width: 100%;
		border: none !important;
		border-radius: 0 !important;
		position: relative;
		.owners-icon{
			position: absolute;
			right: 20rpx;
		}
	}
	.u-hairline-border:after {
	   
	    border: none;
		border-top: 2px solid #FFBB1A;
		border-bottom: 2px solid #FFBB1A;
	}
	uni-button:after{
		border-radius: 0 !important;
	}
	.u-wrap {
		height: 100%;
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 230rpx;
		// width: 30%;
		height: 100%;
		background-color: #FFFFFF;
	}

	.u-tab-item {
		width: 230rpx;
		height: 80rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: flex-start;
		padding-left: 35rpx;
		font-size: 30rpx;
		// line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: $uni-text-color;
		font-size: 30rpx;
		// font-weight: 600;
		// background: #fff;
		background-color: $theme-color;
	}

	// .u-tab-item-active::before {
	// 	content: "";
	// 	position: absolute;
	// 	border-left: 4px solid $u-type-primary;
	// 	height: 32rpx;
	// 	left: 0;
	// 	top: 39rpx;
	// }

	.right-box {
		// background-color: rgb(250, 250, 250);
	}

	.page-view {
		padding: 16rpx;
	}

	.class-item {
		margin-bottom: 30rpx;
		// background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}

	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}

	.item-menu-name {
		// 两行省略
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 1;
		line-clamp: 1;
		-webkit-box-orient: vertical;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		width: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 48rpx;
		// margin: 48rpx 1vw 0 1vw;
		padding-left: 3rpx;
		padding-right: 3rpx;
	}

	.item-menu-image {
		width: 170rpx;
		height: 190rpx;
	}
</style>
